unit uFormLoad;

{$mode objfpc}{$H+}

interface

uses
    Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    BGRAFlashProgressBar;

type

    { TfrmLoad }

    TfrmLoad = class(TForm)
        mmoLoad: TMemo;
    private
        fCurrentState: string;

        function GetStateString(const aCaption: string; const aProgress: Integer): string;
    public
        procedure SetState(const aCaption: string);
        procedure UpdateState(const aProgress: Integer);
    end;

function Show: TfrmLoad;

implementation

function Show: TfrmLoad;
begin
    Result := TfrmLoad.Create(Application);
    Result.DoubleBuffered := True;
    Result.AlphaBlend := True;
    Result.AlphaBlendValue := 220;
    Result.BorderStyle := bsNone;
    Result.Left := Screen.DesktopLeft;
    Result.Top := Screen.DesktopTop;
    Result.mmoLoad.ReadOnly := True;
    Result.mmoLoad.WordWrap := False;
    Result.mmoLoad.Lines.Clear;
    Result.mmoLoad.ScrollBars := ssNone;
    Result.mmoLoad.ParentFont := True;
    Result.mmoLoad.DoubleBuffered := True;
    //Result.Font.Name := 'Anonymous Pro';
    Result.Font.Name := 'Liberation Mono';
    Result.Font.Size := 7;
    Result.Font.Color := $007ED181;
    Result.Font.Style := [fsBold];
    Result.Width := Result.Canvas.TextWidth('1234567890123456 [..........] ');
    Result.Height := Result.Canvas.TextHeight('0') + 2;
    Result.Show;
end;

{$R *.lfm}

{ TfrmLoad }

function TfrmLoad.GetStateString(const aCaption: string;
    const aProgress: Integer): string;
const
    //        123456789012345678901234567890
    cCompl = '                 [completed!]';
var
    s: string;
    i, c: integer;
begin
    s := cCompl;
    for i := 1 to Length(aCaption) do
        s[i] := aCaption[i];
    c := aProgress div 10;
    if c > 10 then
        c := 10;
    for i := 28 downto c + 19 do
        s[i] := '.';
    Result := s;
end;

procedure TfrmLoad.SetState(const aCaption: string);
begin
    Self.Height := Canvas.TextHeight('0') * (mmoLoad.Lines.Count + 1) + 2;
    FCurrentState := aCaption;
    mmoLoad.Lines.BeginUpdate;
    mmoLoad.Lines.Add(GetStateString(FCurrentState, 0));
    mmoLoad.Lines.EndUpdate;
    Application.ProcessMessages;
end;

procedure TfrmLoad.UpdateState(const aProgress: Integer);
begin
    mmoLoad.Lines.BeginUpdate;
    mmoLoad.Lines[mmoLoad.Lines.Count - 1] := GetStateString(FCurrentState, aProgress);
    mmoLoad.Lines.EndUpdate;
    Application.ProcessMessages;
end;

end.

